<?php

namespace Trawind\Rocketmq\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Symfony\Component\Console\Input\InputOption;
use Trawind\Basics\Enums\ServiceDomain;
use Trawind\Basics\Traits\TenantCommand;
use Trawind\ConfigCenter\Middleware\ConfigVersion;
use Trawind\Helpers\HttpClientHelper;
use Trawind\Remotes\Repositories\Account\Config\CommonRepositoryEloquent;
use TrawindCloud\Jobs\Products\ProductSkuFilterJob;

class ListenCommand extends Command
{

    /**
     * The console command name.
     *
     * @example php artisan rocketmq:listen  --queue=product_sku_recheck --tenant_id=100000
     * @var string
     */
    protected $signature = 'rocketmq:listen
                            {--tenant_id= : tenant_id}
                            {--delay=0 : The number of seconds to delay failed jobs}
                            {--force : Force the worker to run even in maintenance mode}
                            {--memory=128 : The memory limit in megabytes}
                            {--queue= : The queue to listen on}
                            {--sleep=3 : Number of seconds to sleep when no job is available}
                            {--timeout=0 : The number of seconds a child process can run}
                            {--tries=1 : Number of times to attempt a job before logging it failed}';


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $tenantId = $this->option('tenant_id');
        $tenant = config('tenancy.tenant_model')::where('tenant_id', '=', $tenantId)->firstOrFail();

        $tenant->run(function ($model) use ($tenantId) {
            app('config')->set('queue.connections.rocketmq.group_id', 'GID_'.$tenantId);

            $this->call('queue:listen', [
                'connection' => 'rocketmq',
                '--queue' => $this->option('queue'),
                '--timeout' => $this->option('timeout'),
            ]);
        });
    }
}
